`timescale 1ns / 1ps

//////////////////////////////////////////////////////////////////////
////                                                              //// 
////                                                              //// 
////  Parte del proyecto "Controlador de luces de un semaforo"    ////  
////                                                              ////
////                                                              //// 
////  Description                                                 //// 
////  	- Modulo central que comunica a todos lo otros modulos   ////
///      los une mediente wire para su respectiva comunicacion    ////
////                                                              //// 
////  To Do:                                                      //// 
////                                                              //// 
////  Author(s):                                                  //// 
////      - Sergio Gonzalez Q, sergiogq@hotmail.es                ////
////      - Alejandro Morales A, ale3191@gmail.com                //// 
////                                                              //// 
////////////////////////////////////////////////////////////////////// 
////                                                              //// 
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 //// 
////                                                              //// 
//// This source file may be used and distributed without         //// 
//// restriction provided that this copyright statement is not    //// 
//// removed from the file and that any derivative work contains  //// 
//// the original copyright notice and the associated disclaimer. //// 
////                                                              //// 
//// This source file is free software; you can redistribute it   //// 
//// and/or modify it under the terms of the GNU Lesser General   //// 
//// Public License as published by the Free Software Foundation; //// 
//// either version 2.1 of the License, or (at your option) any   //// 
//// later version.                                               //// 
////                                                              //// 
//// This source is distributed in the hope that it will be       //// 
//// useful, but WITHOUT ANY WARRANTY; without even the implied   //// 
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //// 
//// PURPOSE.  See the GNU Lesser General Public License for more //// 
//// details.                                                     //// 
////                                                              //// 
//// You should have received a copy of the GNU Lesser General    //// 
//// Public License along with this source; if not, download it   //// 
//// from http://www.opencores.org/lgpl.shtml                     //// 
////                                                              ////
//////////////////////////////////////////////////////////////////////

/**
- Este modulo esta conectado directamente son el Spartam C, donde tiene como
entradas el reloj que tiene internamente este, el reset, el walk_Request y el 
reprogram que son botones; el sensor, el time_parameter_select, el time_value
 que son switches
- Las salidas que son LEDS (salidas de los semafaros y la de walk); el segment_o es el
tiempo restante que le queda al usuario para cruzar la calle y second_o que es el reloj 
de un segundo 
*/

module SemaphoreControlLight(clk_i,rst_i, time_value_i, time_parameter_selector_i, reprogram_i, 
										walk_request_i, sensor_i,segment_o, LDEs_o,second_o);

	//Imputs
	input clk_i, rst_i, sensor_i, walk_request_i,reprogram_i;
	input [3:0] time_value_i; // valor a ingresar
	input [1:0] time_parameter_selector_i; // selecciona el parametro a programar
	
	//Outputs
	output wire [3:0] segment_o; // tiempo restante   
	output wire [6:0] LDEs_o; // salida de los semafaros y  la de walk (indica que al usuario que puede cruzar la calle)
	output wire second_o; // salida de un segundo
	
	// wire controller
	wire wire_sensor_sync_o,wire_wr_sync_o,wire_prog_sync_o,wire_expired_o;
	wire wire_WR_rst_i,wire_WR_o,wire_start_timer_o, wire_walk_request_fsm_o;
	wire [3:0] wire_value_i;
	wire [1:0] wire_interval_i;
	
	// cominicacion de todos los modulos
	Synchronizer synchronizer(.clk_i(clk_i),.rst_i(rst_i),.sensor_i(sensor_i),.walk_request_i(walk_request_i),
					.reprogram_i(reprogram_i),.sensor_sync_o(wire_sensor_sync_o),
					.wr_sync_o(wire_wr_sync_o),.prog_sync_o(wire_prog_sync_o));
	
	Walk_Register walk_Register(.clk_i(clk_i),.rst_i(rst_i),.request_i(wire_wr_sync_o), .WR_rst_i(wire_WR_rst_i), 
											.WR_o(wire_WR_o) );
	
	FSM fsm(.clk_i(clk_i),.rst_i(rst_i), .Sensor_Sync_i(wire_sensor_sync_o), .WR_i(wire_WR_o), .Prog_Sync_i(wire_prog_sync_o),
				.expired_i(wire_expired_o),.WR_rst_o(wire_WR_rst_i), .interval_o(wire_interval_i), .LDEs_o(LDEs_o) ,
				.start_timer_o(wire_start_timer_o),.walk_request_fsm_o(wire_walk_request_fsm_o));
		
		
	Time_Parameters time_Parameters(.clk_i(clk_i),.rst_i(rst_i), .time_parameter_selector_i(time_parameter_selector_i), 
			.time_value_i(time_value_i), .reprogram_i(wire_prog_sync_o), .interval_i(wire_interval_i), .value_o(wire_value_i) );
	
	Timer timer(.clk_i(clk_i),.rst_i(rst_i), .hz_enable_clk_i(hz_enable_clk_o), .value_i(wire_value_i), 
					.start_timer_i(wire_start_timer_o), .walk_request_fsm_i(wire_walk_request_fsm_o), .expired_o(wire_expired_o), 
					.segment_o(segment_o));
	
	Divider divider(.clk_i(clk_i),.rst_i(rst_i), .hz_enable_clk_o(hz_enable_clk_o),.second_o(second_o));
	
	
endmodule
